<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/Location.jsx或Location.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :rowClick="rowClick"
        :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import extend from "@/extension/mes/mes/Location.jsx";
import viewOptions from './Location/options.js'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    gridRef.load = false

    gridRef.sortable = true

    gridRef.pagination.sortName = "StockId";  //明细表排序字字段
    gridRef.pagination.order = "asc"; //明细表排序方式desc或者asc
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })
    gridRef.height = 200;
    gridRef.buttons.forEach(x => {
        if (x.name == '高级查询' || x.name == '审核' || x.name == '反审' || x.name == '编辑') {
            x.hidden = true;
        }
    })
    gridRef.buttons.splice(4, 0, {
        name: '禁用', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {
            locationDisabled()
        }
    })
    gridRef.buttons.splice(5, 0, {
        name: '反禁用', //按钮名称
        icon: 'el-icon-document', //按钮图标:组件示例->图标
        //primary、success、warning、error、info、text、danger
        type: 'danger',
        plain: true,
        onClick: () => {
            unLocationDisabled()
        }
    })
}
const searchBefore = async (param) => {
    const stockRows = getStockSelectRow();
    if (!stockRows.length) {
        proxy.$message.error('请先选择【仓库】数据')
        return false;
    }
    //查询前获取bom的id，查询明细表
    param.wheres.push({ name: "StockId", value: stockRows[0].StockId })
    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    const StockId = getStockSelectRow()[0].StockId;
    formData.mainData['StockId'] = StockId
    formData.mainData['IsEnabled'] = 1
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const getStockSelectRow = () => {
    const fn = proxy.base.getItem('getStockSelectRow');
    return fn();
}
const locationDisabled = (rows) => {
    var rows = gridRef.getSelectRows()

    if (rows.length == 0) {
        proxy.$message.error("请选择仓位！");
        return
    }

    let url = 'api/Location/locationDisabled';

    proxy.http.post(url, rows).then((result) => {
        proxy.$message.success("仓位禁用！");
        gridRef.search();
        // 刷新主表列表
    });
}
const unLocationDisabled = (rows) => {
    var rows = gridRef.getSelectRows()

    if (rows.length == 0) {
        proxy.$message.error("请选择仓位！");
        return
    }

    let url = 'api/Location/unLocationDisabled';

    proxy.http.post(url, rows).then((result) => {
        proxy.$message.success("仓位启用！");
        gridRef.search();
        // 刷新主表列表
    });
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    grid.value.clearSelection();
    //点击行默认选中
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    const stockRows = getStockSelectRow();

    if (!stockRows.length > 1) {
        proxy.$message.error('只能选择一行仓库数据！')
        return false;
    }

    for (let index in stockRows) {
        if (stockRows[index].AuditStatus == 0 || stockRows[index].AuditStatus == null) {
            proxy.$message.error(stockRows[index].Name + '【仓库】未审核')
            return false;
        }
        if (stockRows[index].IsLocation != 1) {
            proxy.$message.error(stockRows[index].Name + '【仓库】未启用仓位')
            return false;
        }
    }

    if (!stockRows.length) {
        proxy.$message.error('请先选择【仓库】数据')
        return false;
    }
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {
    //弹出框打开后方法,设置表单默认值,按钮操作等
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
